﻿@page "/Production/EnergyConsumptions"
@using BootstrapAdmin.Web.Core.Services.Production
@using BootstrapAdmin.Web.ViewModels.Production
@inject IEnergyConsumptionService energyConsumptionService

<AdminTable TItem="EnergyConsumption" IsMultipleSelect="true" IsPagination="true" PageItemsSource="PageItemsSource"
    ShowDefaultButtons="false" ShowExtendButtons="false"
    ShowSearch="true" SearchMode="SearchMode.Top" CustomerSearchModel="SearchModel"
    OnQueryAsync="@OnQueryAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.Energy"></TableColumn>
        <TableColumn @bind-Field="@context.CreatedAt"></TableColumn>
    </TableColumns>
    <CustomerSearchTemplate>
        @if (context is EnergyConsumptionSearchModel searchModel)
        {
            <div class="row g-3 form-inline">
                <div class="col-sm-6 col-md-6">
                    <DateTimeRange @bind-Value="@searchModel.CreatedTime" ShowLabel="true" AllowNull="true" />
                </div>
            </div>
        }
    </CustomerSearchTemplate>
</AdminTable>

@code {
    private static IEnumerable<int> PageItemsSource => new int[] { 20, 50, 100 };

    private EnergyConsumptionSearchModel SearchModel = new();

    private Chart? LineChart;

    private async Task<QueryData<EnergyConsumption>> OnQueryAsync(QueryPageOptions options)
    {
        var filter = new EnergyConsumptionFilter
        {
            CreatedTimeStart = SearchModel.CreatedTime?.Start,
            CreatedTimeEnd = SearchModel.CreatedTime?.End,
        };

        var (items, count) = await energyConsumptionService.GetPagedAsync(filter, options.PageIndex, options.PageItems);

        return new QueryData<EnergyConsumption>()
        {
            Items = items,
            TotalCount = count,
            IsSorted = true,
            IsFiltered = true,
            IsSearch = true,
            IsAdvanceSearch = true,
        };
    }
}
